<template>
  <el-dialog title="新增员工" :visible="showDialog" :model="formData" @close="handleClose" @open="handleOpen">
    <!-- 表单 -->
    <el-form label-width="120px">
      <el-form-item label="姓名">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="聘用形式">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in hireTpyes" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />

      </el-form-item>
      <el-form-item label="部门">
        <!-- readonly:只读
            disabled:禁用 -->
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" readonly @focus="isShow=true" />
        <el-tree v-if="isShow" :data="deptTreeDate" :props="{label:'name'}" @node-click="handleNodeClick" />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" value-format="yyyy-MM-dd" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleAddEmp">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { addEmpAPI } from '@/api/employees'
import { depListAPI } from '@/api/departments'
import { list2Tree } from '@/utils/shift'
// 导入员工常量模块大对象
import obj from '@/constant/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      hireTpyes: obj.hireType,
      deptTreeDate: [],
      // 控制树形组件的显示隐藏
      isShow: false
    }
  },
  created() {
    // console.log(this.hireTpyes)
  },
  methods: {
    handleClose() {
      this.$emit('update:showDialog', false)
    },
    async handleOpen() {
      // 发起获取部门列表请求
      const resp = await depListAPI()
      // 转为树形结构
      this.deptTreeDate = list2Tree(resp.data.depts, '')
    },
    // 树形节点点击时
    handleNodeClick(node) {
      // node：被点击的部门对象
      // 如果有子部门，直接结束
      if (node.children) return
      this.formData.departmentName = node.name
      this.isShow = false
    },
    async handleAddEmp() {
      const resp = await addEmpAPI(this.formData)
      console.log(resp)
      // 关闭弹框
      this.handleClose()
      // 获取员工列表
      // this.$parent.getEmpList() 拿到父组件的方法
      // 推荐子传父
      this.$emit('add-emp-suc')
    }
  }
}
</script>

  <style lang="scss" scoped>
  .el-tree {
    position: absolute;
    width: 50%;
    min-height: 50px;
    left: 0;
    top: 45px;
    z-index: 100;
    border: 1px solid #ccc;
    border-radius: 4px;
    padding-right: 5px;
    overflow: hidden;
    background-color: #fff;
    max-height: 200px;
    overflow: auto;
    ::v-deep {
      .el-tree-node__content {
        height: auto;
      }
    }
  }
  </style>
